//
//  CZQAddStudyExperView.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/14.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQAddStudyExperView.h"

@implementation CZQAddStudyExperView
-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        self.scrollView.backgroundColor = CSWhiteColor;
        
        /**
         头像上传
         */
        UIButton *photoBtn = [[UIButton alloc] init];
        photoBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        photoBtn.imageView.layer.masksToBounds = YES;
        photoBtn.layer.masksToBounds = YES;
        photoBtn.layer.cornerRadius = 40;
        [photoBtn setImage:[UIImage imageNamed:@"img_pictureloading"] forState:UIControlStateNormal];
        [self.scrollView addSubview:photoBtn];
        self.photoBtn = photoBtn;
        
        /**
         上传头像
         */
        UILabel *uploadLabel = [[UILabel alloc] init];
        uploadLabel.font = MidFont;
        uploadLabel.textColor = PlaceHoldColor;
        uploadLabel.text = @"机构LOGO";
        [self.scrollView addSubview:uploadLabel];
        self.LOGOLabel = uploadLabel;
        
        /**
         机构名称
         */
        CommonInputView *companyInputView = [[CommonInputView alloc] init];
        companyInputView.titleLabel.text = @"培训机构*";
        companyInputView.titleTF.placeholder = @"培训机构";
        [scrollView addSubview:companyInputView];
        self.companyView = companyInputView;
        
        /**
         培训内容
         */
        CommonInputView *jobNameInputView = [[CommonInputView alloc] init];
        jobNameInputView.titleLabel.text = @"培训内容*";
        jobNameInputView.titleTF.placeholder = @"培训内容";
        [scrollView addSubview:jobNameInputView];
        self.studyTitleView = jobNameInputView;
        
       
        
        /**
         起始时间
         */
        CommonBtnView *startBtnView = [[CommonBtnView alloc] init];
        startBtnView.titleLabel.text = @"起始时间*";
        startBtnView.choseLabel.text = @"选择起始时间";
        startBtnView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:startBtnView];
        self.startTimeBtnView = startBtnView;
        
        /**
         结束时间
         */
        CommonBtnView *endBtnView = [[CommonBtnView alloc] init];
        endBtnView.titleLabel.text = @"结束时间*";
        endBtnView.choseLabel.text = @"选择结束时间";
        endBtnView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:endBtnView];
        self.endTimeBtnView = endBtnView;
        
        
        //有无证书
        
        
        CommentSwitchView *cerSwichView = [[CommentSwitchView alloc] init];
        cerSwichView.titleLabel.text = @"有无证书";
        [cerSwichView.titleSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        [scrollView addSubview:cerSwichView];
        self.cerSwitchView = cerSwichView;
        
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     头像上传
     */
    [self.photoBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.scrollView).offset(30);
        
        make.centerX.mas_equalTo(self.scrollView);
        
        make.width.height.mas_equalTo(80);
    }];
    
    /**
     上传头像
     */
    [self.LOGOLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.photoBtn);
        
        make.top.mas_equalTo(self.photoBtn.mas_bottom).offset(12);
    }];
    
    //公司名称
    [self.companyView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.LOGOLabel.mas_bottom).offset(30);
        
        make.left.width.mas_equalTo(self.scrollView);
    }];
    
    //工作岗位
    [self.studyTitleView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.companyView.mas_bottom);
        
        make.left.width.mas_equalTo(self.scrollView);
    }];
    
    //起始时间
    [self.startTimeBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.studyTitleView.mas_bottom);
        
        make.left.width.mas_equalTo(self.scrollView);
    }];
    
    //结束时间
    [self.endTimeBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.startTimeBtnView.mas_bottom);
        
        make.left.width.mas_equalTo(self.scrollView);
    }];
    
    //上传图片
    [self.cerSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.endTimeBtnView.mas_bottom).offset(0);
        
        make.left.width.mas_equalTo(self.scrollView);
    }];
    
    //有证书
    if (self.cerSwitchView.titleSwitch.isOn) {
        
        [self.cerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.cerSwitchView.mas_bottom).offset(-60);
            
            make.left.width.mas_equalTo(self.scrollView);
            
            make.height.mas_equalTo(400);
        }];
        
    }
    
    
    [self setNeedsLayout];
    
    [self layoutIfNeeded];
    
}

#pragma mark - 有证书


-(void)switchAction:(UISwitch *)swich
{
    WS(weakSelf)
    
    if (swich.isOn) {
        
        CZQAddCerView *cerView = [CZQAddCerView new];
        
        [self.scrollView addSubview:cerView];
        
        cerView.scrollView.showsVerticalScrollIndicator = NO;
        
        self.cerView = cerView;
        
        [self.cerView.startTimeBtnView.choseBtn bk_addEventHandler:^(id sender) {
            
            [self endEditing:YES];
            
            
            [[MOFSPickerManager shareManger] showDatePickerWithTag:68 title:@"选择生效时间"cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
                
                //处理
                NSDateFormatter *objDateformat1 = [[NSDateFormatter alloc] init];
                [objDateformat1 setDateFormat:@"yyyy-MM-dd"];
                
                NSString *timestamp1 = [objDateformat1 stringFromDate: date];
                
                weakSelf.cerView.startTimeBtnView.choseLabel.text = timestamp1;
                
                weakSelf.cerView.startTimeBtnView.choseLabel.textColor = TFColor;
                
            } cancelBlock:^{
                
            }];
            
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        //证书失效效时间
        [self.cerView.endTimeBtnView.choseBtn bk_addEventHandler:^(id sender) {
            
            [self endEditing:YES];
            
            
            [[MOFSPickerManager shareManger] showPickerViewWithDataArray:@[@"选择时间",@"长期有效"] tag:67 title:@"选择失效" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
                
                if ([string isEqualToString:@"长期有效"]) {
                    
                    weakSelf.cerView.endTimeBtnView.choseLabel.text = @"长期有效";
                    
                    weakSelf.cerView.endTimeBtnView.choseLabel.textColor = TFColor;
                    
                }else
                {
                    
                    [[MOFSPickerManager shareManger] showDatePickerWithTag:59 title:@"选择失效时间"cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
                        
                        //处理
                        NSDateFormatter *objDateformat1 = [[NSDateFormatter alloc] init];
                        [objDateformat1 setDateFormat:@"yyyy-MM-dd"];
                        
                        NSString *timestamp1 = [objDateformat1 stringFromDate: date];
                        
                        weakSelf.cerView.endTimeBtnView.choseLabel.text = timestamp1;
                        
                        weakSelf.cerView.endTimeBtnView.choseLabel.textColor = TFColor;
                        
                    } cancelBlock:^{
                        
                    }];
                    
                }
                
            } cancelBlock:^{
                
            }];
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        
        //上传证书
        [self.cerView.cerPhotoBtn bk_addEventHandler:^(id sender) {
            
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(uploadCerPoto:)]) {
                [weakSelf.delegate uploadCerPoto:sender];
            }
            
//            [weakSelf logoSelect:sender];
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView sendSubviewToBack:cerView];
        
    }else
    {
        
        [self.cerView removeFromSuperview];
        
    }
    
    [self setConstraints];
    
    [self setNeedsLayout];
    [self setNeedsDisplay];
    
}

#pragma mark - 图片选择

-(void)logoSelect:(UIButton *)photoSelect{
    
    
    WS(weakSelf);
    
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
    
    pickerVc.maxCount = 1;
    
    // Jump AssetsVc
    pickerVc.status = PickerViewShowStatusCameraRoll;
    // Recoder Select Assets
    //    pickerVc.selectPickers = self.assets;
    // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
    
    pickerVc.photoStatus = PickerPhotoStatusPhotos;
    // Desc Show Photos, And Suppor Camera
    pickerVc.isShowCamera = YES;
    pickerVc.topShowPhotoPicker = YES;
    
    // CallBack
    
    pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
        
        for (ZLPhotoAssets *assets in status) {
            
            ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:assets];
            
            photo.asset = assets;
            
            
            //拍照
            if ([photo.asset isKindOfClass:[UIImage class]]) {
                
                [photoSelect setImage:(UIImage *)photo.asset forState:UIControlStateNormal];
            }
            else{
                
                [photoSelect setImage:[assets thumbImage] forState:UIControlStateNormal];
                
            }
            
            //                    [weakSelf uploadImage];
            
            break;
        }
    };
    
    [pickerVc showPickerVc:self.viewController];
    
}

-(void)layoutSubviews{
    
    
    if (self.cerSwitchView.titleSwitch.isOn) {
        self.scrollView.contentSize = CGSizeMake(self.width, self.cerSwitchView.bottom + 340 + CZQ_StatusBarHeight + CZQ_NavigationBarHeight);
    }else
    {
        self.scrollView.contentSize = CGSizeMake(self.width, self.cerSwitchView.bottom + CZQ_StatusBarHeight + CZQ_NavigationBarHeight);
        
    }
    
    NSLog(@"%f==%f",self.cerSwitchView.bottom,self.cerView.bottom);
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
